The IE Web Browser Control and Applying Styles

Mark Leighton Fisher on 2006-02-03T17:31:43

Applying styles – or rather trying to apply styles – to HTML destined to live inside the IE Web Browser Control can be problematic. If you generate the HTML dynamically into a string to be displayed by the control, then you lose the capability to apply global styles (styles using CLASS or element name definitions in the HEAD section). This is because the Microsoft HTML Object Library (mshtml.dll) used by the IE Web Browser Control only supports assigning BODY element contents as a string. Now, the HTML string *can* have a HEAD section -- it is just ignored by the control, which (IMHO) confuses the matter.

The HTML-only solution is to write the HTML to a temporary file, then display that file. Writing the string to a file to be displayed also helps you use stylesheets, as they are also specified in the HEAD section.

Here is an example of setting global styles in code when you load the HTML as a string:

objCSS = wb.Document.createStyleSheet
objCSS.addRule "H1", "{ font-size: 120%; line-height: 10%; }"
objCSS.addRule "BR", "{ font-size: 80%; line-height: 20%; }"
objCSS.addRule "TR", "{ font-size: 80%; line-height: 10%; }"
objCSS.addRule "TH", "{ font-size: 80%; line-height: 10%; }"
objCSS.addRule "TD", "{ font-size: 80%; line-height: 10%; }"
objCSS.addRule "DIV", "{ font-size: 80%; line-height: 10%; }"